package com.example.nonacademic_education.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.nonacademic_education.dao.AdminDao;
import com.example.nonacademic_education.dao.ProjectDao;
import com.example.nonacademic_education.dao.StudentDao;
import com.example.nonacademic_education.dao.TheClassDao;
import com.example.nonacademic_education.data.request.TheClassList;
import com.example.nonacademic_education.data.response.TheClassAllDto;
import com.example.nonacademic_education.data.response.TheClassDto;
import com.example.nonacademic_education.entity.TheClass;
import com.example.nonacademic_education.service.TheClassService;
import com.example.nonacademic_education.util.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:yanghua
 * @Date:2023/4/18 22:28
 */
@Service
public class TheClassServiceImpl extends ServiceImpl<TheClassDao, TheClass> implements TheClassService {
    @Resource
    private TheClassDao theClassDao;
    @Resource
    private ProjectDao projectDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private AdminDao adminDao;
    @Override
    public R addOne(TheClass theClass) {
        if (theClass.getId()!=null){
            return R.error("id is not null");
        }
        Map<String,Object> data = new HashMap<>();
        String projectName= projectDao.getNameById(theClass.getProjectId());
        projectName=projectName+" "+theClass.getYear()+"级"+theClass.getSeq()+"班";
        theClass.setName(projectName);
        theClassDao.insert(theClass);
        data.put("theClass",theClass);
        return R.ok(data,"addOne");
    }

    @Override
    public R deleteOne(Long id) {
        Integer i = theClassDao.deleteOne(id);
        studentDao.deleteByClassId(id);
        return R.ok("删除成功");
    }

    @Override
    public R updateOne(TheClass theClass) {
        if (theClass.getId()==null){
            return R.error("id is null");
        }
        Map<String,Object> data = new HashMap<>();
        theClassDao.updateById(theClass);
        data.put("theClass",theClass);
        return R.ok(data,"修改成功");
    }

    @Override
    public R getLstList(TheClassList theClassList) {
        if (theClassList.getPage() < 1) {
            return R.error("页数不能小于1");
        }
        if (theClassList.getDepartmentId()==null){
            theClassList.setDepartmentId(adminDao.getDepartmentIdByUsername(StpUtil.getLoginIdAsString()));
        }
        theClassList.setPage((theClassList.getPage()-1)*theClassList.getSize());
        List<TheClassAllDto> list = theClassDao.getList(theClassList);
        Integer total = theClassDao.getNumber(theClassList);
        Map<String,Object> data = new HashMap<>();
        data.put("list",list);
        data.put("total",total);
        return R.ok(data,"查询成功");
    }

    @Override
    public R getIdAndName(TheClassList theClassList) {
        if (theClassList.getDepartmentId()==null){
            theClassList.setDepartmentId(adminDao.getDepartmentIdByUsername(StpUtil.getLoginIdAsString()));
        }
        List<TheClassDto> list = theClassDao.getIdAndName(theClassList);
        Integer total = theClassDao.getNumber(theClassList);
        Map<String,Object> data = new HashMap<>();
        data.put("list",list);
        data.put("total",total);
        return R.ok(data,"查询成功");
    }
}
